<template>
  <div style="padding: 10px 5px">
    <el-form ref="form" v-loading="loading" size="small" :model="form" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="名称">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="价格">
            <el-input v-model="form.price"></el-input>
          </el-form-item>
          <el-form-item label="图标">
            <upload-input ref="uploadInput"></upload-input>
          </el-form-item>
          <el-form-item label="地址">
            <el-input v-model="form.address"></el-input>
          </el-form-item>
          <el-form-item label="联系方式">
            <el-input type="textarea" v-model="form.contact"></el-input>
          </el-form-item>
          <el-form-item label="代理">
            <el-input v-model="form.contact"></el-input>
          </el-form-item>
          <el-form-item label="城市">
            <el-input v-model="form.city"></el-input>
          </el-form-item>
          <el-form-item label="专业">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="18">
          <el-form-item label="详情">
            <tinymce ref="tinymce" v-model="form.details" :height="300"></tinymce>
          </el-form-item>
          <el-form-item>
            <el-button @click="onSubmit()" type="primary">立即保存</el-button>
            <el-button @click="$router.push({ name: 'investigation' })">返回</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import UploadInput from '@/components/UploadInput'
import Tinymce from '@/components/Tinymce'
import { getDetails, create } from '@/api/investigation'

export default {
  props: {
    id: {
      type: Number,
      default: 0
    }
  },
  components: {
    UploadInput,
    Tinymce
  },
  data() {
    return {
      form: {
        name: null,
        price: null
      },
      loading: false
    }
  },
  created() {
    this.getDetails()
  },
  methods: {
    onSubmit() {
      this.create()
    },
    getDetails() {
      this.loading = true
      getDetails(this.id).then(res => {
        this.loading = false
        if (res.status === 200) {
          this.form = res.data
        }
      })
    },
    // 新建
    create() {
      create(this.form).then(res => {
        if (res.status === 200) {
          this.$message({
            type: 'success',
            message: res.message
          })
        } else {
          this.$message({
            type: 'error',
            message: res.message
          })
        }
      })
    }
  }
}
</script>